package com.eastfair.projectcore.service.impl;

import cn.hutool.core.util.StrUtil;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.projectcore.dao.DictMapper;
import com.eastfair.projectcore.entity.Dict;
import com.eastfair.projectcore.service.DictService;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.common.DictionaryTypeCacheKeyBuilder;
import com.eastfair.cache.projectcore.DictCacheKeyBuilder;
import com.eastfair.cache.repository.CachePlusOps;
import com.eastfair.core.base.R;
import com.eastfair.core.model.CacheHashKey;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.core.utils.CollHelper;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.LbqWrapper;
import com.eastfair.echo.properties.EchoProperties;
import com.eastfair.uid.model.SelfUidDto;
import com.eastfair.uid.service.SelfUidService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;

/**
 * <p>
 * 业务实现类
 * 字典表
 * </p>
 *
 * @author ligang
 * @date 2021-07-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DictServiceImpl extends SuperCacheServiceImpl<DictMapper, Dict> implements DictService {

    private final EchoProperties ips;
    private final CachePlusOps cachePlusOps;
//    @Autowired
//    SelfUidService selfUidService;

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new DictCacheKeyBuilder();
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(Dict model) {
        //雪花ID,不需要分库分表
//        SelfUidDto selfUidDto = new SelfUidDto();
//        selfUidDto.setDbSuffix(0);
//        selfUidDto.setTableSuffix(0);
//        model.setId(selfUidService.getUid(selfUidDto));
        model.setId(DefaultUidGenerator.getUid(0L));
        return R.successDef();
    }


    @Override
    public Map<Serializable, Object> findNameByIds(Set<Serializable> types) {
        if (types.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<Serializable, Object> codeValueMap = new HashMap<>();
//        types.forEach(type -> {
//            LbqWrapper<Dict> wrap = Wraps.<Dict>lbQ().eq(Dict::getDictTypeCode, type);
//            List<Dict> list = list(wrap);
//            Map<String, String> map= CollHelper.uniqueIndex(list, Dict::getCode, Dict::getName);
//            map.forEach((value, txt) -> codeValueMap.put(StrUtil.join(ips.getDictSeparator(), type, value), txt));
//        });
        types.forEach(type -> {
            Function<CacheHashKey, Map<String, String>> fun = (ck) -> {
                LbqWrapper<Dict> wrap = Wraps.<Dict>lbQ().eq(Dict::getDictTypeCode, type);
                List<Dict> list = list(wrap);
                return CollHelper.uniqueIndex(list, Dict::getCode, Dict::getName);
            };
            Map<String, String> map = cachePlusOps.hGetAll(new DictionaryTypeCacheKeyBuilder().hashKey(type), fun);
            map.forEach((value, txt) -> codeValueMap.put(StrUtil.join(ips.getDictSeparator(), type, value), txt));
        });
        return codeValueMap;
    }

    @Override
    public Map<Serializable, Object> findByIds(Set<Serializable> set) {
        return Collections.emptyMap();
    }

}
